package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.FacilitiesClassDao;
import cn.topevery.garden.entity.po.FacilitiesClass;
import cn.topevery.garden.entity.query.FacilitiesClassQuery;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.entity.SqlUpdateMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author code-generate
 * @date 2021-08-06
 */
@Repository
public class FacilitiesClassDaoImpl extends BaseDaoImpl<FacilitiesClass> implements FacilitiesClassDao {
    @Override
    public <P extends DbCsp> List<FacilitiesClass> getSearch(P para) {
        return null;
    }


    @Override
    public DataPager<FacilitiesClass> listPage(FacilitiesClassQuery query) {
        String table = "t_facilities_class tfc";

        String columns = "tfc.*";
        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tfc.c_id desc");
        sqlQueryMate
                .where(true, "tfc.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getCode()), "tfc.c_code", ScriptConditionEnum.Like, query.getCode())
                .where(StringUtils.isNotBlank(query.getTypeName()), "tfc.c_name", ScriptConditionEnum.Like, query.getTypeName())
                .where(query.getStatus() != null, "tfc.c_status", ScriptConditionEnum.Eq, query.getStatus());

        return getDataPager(sqlQueryMate, pageSize, pageIndex, FacilitiesClass.class);
    }

    @Override
    public List<FacilitiesClass> getListByPid(String pid) {
        String table = "t_facilities_class tfc";

        String columns = "tfc.*";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "tfc.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tfc.c_parent_id", ScriptConditionEnum.Eq, pid)
        ;
        return selectList(sqlQueryMate, FacilitiesClass.class);
    }

    @Override
    public void delByEntityId(Integer userId, String entityId) {
        String table = "t_facilities_class tfc";

        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate(table);
        sqlUpdateMate
                .setColumnValue(true, "tfc.c_db_status", 1)
                .setColumnValue(true, "tfc.c_db_last_update_id", userId)
                .where(true, "tfc.c_entity_id", ScriptConditionEnum.Eq, entityId);
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }
}
